package cn.tedu.medical.passport.controller;

import cn.tedu.medical.passport.pojo.vo.UserLoginResultVO;
import cn.tedu.medical.passport.service.IUserService;
import cn.tedu.medical.common.web.JsonResult;
import cn.tedu.medical.passport.pojo.param.UserLoginInfoParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

import static cn.tedu.medical.common.consts.web.HttpConsts.HEADER_USER_AGENT;

/**
 * 处理用户相关请求的控制器类
 *
 * @author zyb
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/passport")
@Api(tags = "单点登录")
public class UserController {
    @Autowired
    private IUserService userService;
    public UserController() {
        log.info("创建控制器对象：UserController");
    }
    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult<UserLoginResultVO> login(@Validated UserLoginInfoParam userLoginInfoParam,
                                               @ApiIgnore HttpServletRequest request){
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginInfoParam);
        String remoteAddr = request.getRemoteAddr();
        String userAgent = request.getHeader(HEADER_USER_AGENT);
        UserLoginResultVO userLoginResultVO = userService.login(userLoginInfoParam,remoteAddr,userAgent);
        return JsonResult.ok(userLoginResultVO);
    }


}
